<?php
class Core_Common_AclFactory {
  /**
   * Создаёт ACL для указанной страницы
   * @param Page $page
   * @return Zend_Acl
   */
  public function createAcl( $page, $user ) 
  {
    $privileges = Application_Model_PagePrivilege::findByPageId($page->getId());

    $acl = new Zend_Acl();
    $acl->add(new Zend_Acl_Resource($page->getId()));

    foreach($privileges as $privilege) {
		if ( $user->getId() != $privilege->getCallerId() && !in_array( $privilege->getCallerId(), $user->getGroups() ) ){
			continue;
		}
		$acl->addRole($privilege->getCallerId());
		if ( !$acl->hasRole( $user->getId() ) ) {
			$acl->addRole( $user->getId(), $privilege->getCallerId() );
		}
      $acl->allow($privilege->getCallerId(), $page->getId());
    }

    return $acl;
  }
}
?>
